﻿using Christoc.Modules.CouponsDiscounts.Components;
using Christoc.Modules.CouponsDiscounts.Components.Controllers;
using Christoc.Modules.CouponsDiscounts.Data;
using DotNetNuke.Services.Exceptions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Christoc.Modules.CouponsDiscounts
{
    public partial class AddDiscount : CouponsDiscountsModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    PopulateProducts();
                    PopulateDiscounts();

                    dlType.DataSource = Enum.GetNames(typeof(TypeDiscount));
                    dlType.DataBind();

                    //for (int i = 0; i < 4; i++)
                    //{
                    //    dlType.Items.Add(new ListItem(((TypeDiscount)i).ToString(), i.ToString()));
                    //}
                }
            }
            catch(Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }

        }

        private void PopulateDiscounts()
        {
            IDataReader discountsReader = DataProvider.Instance().GetDiscounts();
            lbDiscounts.DataSource = discountsReader;
            lbDiscounts.DataTextField = "Name";
            lbDiscounts.DataValueField = "DiscountID";
            lbDiscounts.DataBind();
        }

        private void PopulateProducts()
        {
            //Populate products into list box for registering disocunts
            IDataReader productsReader = DataProvider.Instance().GetProducts();
            lbProducts.DataSource = productsReader;
            lbProducts.DataTextField = "Name";
            lbProducts.DataValueField = "ProductID";
            lbProducts.DataBind();
        }

        protected void bAdd_Click(object sender, EventArgs e)
        {
            Discount discount;
            discount = new Discount
            {
                Name = tbName.Text.Trim(),
                Value = Convert.ToDouble(tbValue.Text),
                Type = (TypeDiscount)Enum.Parse(typeof(TypeDiscount), dlType.SelectedItem.Text),
                BeginDate = cBeginDate.SelectedDate,
                EndDate = cEndDate.SelectedDate
            };

            int discountID = DiscountController.SaveDiscount(discount);

            foreach (int index in lbProducts.GetSelectedIndices())
            {
                ListItem item = lbProducts.Items[index];
                DiscountController.SaveProductDiscount(discountID, Convert.ToInt32(item.Value));
            }

            //Create and MVC to update views instead
            lbDiscounts.Items.Add(new ListItem(discount.Name, discount.DiscountID.ToString()));

            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
        }

        protected void cEndDate_SelectionChanged(object sender, EventArgs e)
        {
            if (cBeginDate.SelectedDate > cEndDate.SelectedDate)
            {
                lErrorMsg.Visible = true;
                lErrorMsg.Text = "end date should not be less than begin date";
                bAdd.Enabled = false;
            }
            else
            {
                lErrorMsg.Visible = false;
                bAdd.Enabled = true;
            }
        }

    }
}